﻿using Common.Communication;
using Common.Device.DeviceDb;
using Common.Device.Interfaces;
using Common.Tool.DataTools;
using Modbus;
using Modbus.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;

namespace Fct
{
    public class FctModBusSerialPort : ModbusRtuClient, IFct
    {
        byte station;
        public FctModBusSerialPort(UseDevice device) : base(device.name, device.port, Convert.ToInt32(device.parameter), 1500, EndianFormat.ABCD, Convert.ToByte(device.addr, 16), false)
        {
            station = Convert.ToByte(device.addr);
        }
        public Result<bool> GetDp(int index)
        {
            lock (this)
            {
                AppendInfo(Name, $"读取通道{index}电平");
                var result = ReadCoil((0xFF + index).ToString());
                if (result.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = $"读取通道{index}电平失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取通道{index}电平{(result.Value ? "高" : "低")}");
                return result;
            }
        }

        public Result<bool> GetJdq(int index)
        {
            lock (this)
            {
                AppendInfo(Name, $"读取继电器{index}状态");
                var result = ReadCoil((index).ToString());
                if (result.IsSucceed == false)
                {
                    result.IsSucceed = false;
                    result.Err = $"读取继电器{index}状态失败";
                    AppendTip(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取继电器{index}{(result.Value ? "闭合" : "断开")}");
                return result;
            }
        }

        public Result<bool> SetJdq(List<bool?> states)
        {
            lock (this)
            {
                var result = new Result<bool>();
                var write = new Result();
                var read = new Result<byte[]>();
                if (states.Count(o => o == false) == states.Count)
                {
                    int len = states.Count / 8 + (states.Count % 8 > 0 ? 1 : 0);
                    byte[] bytes = new byte[len];
                    AppendInfo(Name, $"设置继电器全断");
                    write = Write("0", bytes, station, 0xF);
                    if (write.IsSucceed == false)
                    {
                        result.IsSucceed = false;
                        result.Err = $"设置继电器全断失败";
                        AppendTip(Name, result.Err);
                        return result.EndTime();
                    }
                }
                else
                {
                    for (var i = 0; i < states.Count; i++)
                    {
                        if (states[i] != null)
                        {
                            AppendInfo(Name, $"设置继电器{i + 1}{states[i]}");
                            write = Write(i.ToString(), (bool)states[i], station);
                            if (write.IsSucceed == false)
                            {
                                result.IsSucceed = false;
                                result.Err = $"设置继电器{i + 1}失败";
                                AppendTip(Name, result.Err);
                                return result.EndTime();
                            }
                        }
                    }
                }
                return result.EndTime();
            }
        }
    }
}
